package com.fjs.scenic.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjs.scenic.controller.common.BaseController;
import com.fjs.scenic.dto.InputInfoDto;
import com.fjs.scenic.dto.InputInfoPageParam;
import com.fjs.scenic.entity.system.InpIncome;
import com.fjs.scenic.entity.system.InputInfo;
import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.entity.system.TypeAuthority;
import com.fjs.scenic.service.system.InpIncomeService;
import com.fjs.scenic.service.system.InputInfoService;
import com.fjs.scenic.service.system.TypeAuthorityService;
import com.fjs.scenic.service.system.impl.ManageServiceImpl;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.security.Principal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * This class is used for ...
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@RestController
@RequestMapping("/inputInfo")
public class InputInfoController {
    @Autowired
    private InputInfoService inputInfoService;
    @Autowired
    private InpIncomeService inpIncomeService;
    @Autowired
    private ManageServiceImpl manageService;
    @Autowired
    private TypeAuthorityService typeAuthorityService;

    @GetMapping("/index")
    public ModelAndView sceniclist() {
        return new ModelAndView("system/inputInfo");
    }

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult add(@Valid @RequestBody List<InputInfoDto> inputInfoDtos, Principal principal) {
        String account = principal.getName();
        Manage manage = manageService.getMangerByAccount(account);
        for (InputInfoDto inputInfoDto : inputInfoDtos) {
            inputInfoDto.setCreateTime(LocalDate.now());
            inputInfoDto.setManager(manage.getName());
            int sum = inputInfoService.count(new LambdaQueryWrapper<InputInfo>()
                    .eq(InputInfo::getStaticDate, inputInfoDto.getStaticDate())
                    .eq(InputInfo::getScenic, inputInfoDto.getScenic()));
            Assert.isTrue(sum == 0, "请勿重复添加");
            inputInfoService.save(inputInfoDto);
            if (inputInfoDto.getIncome() != null) {
                for (InpIncome inpIncome : inputInfoDto.getIncome()) {
                    inpIncome.setInputInfoId(inputInfoDto.getId());
                    inpIncomeService.save(inpIncome);
                }
            }
        }
        return ReturnResult.ok();
    }

    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult edit(@RequestBody List<InputInfoDto> inputInfoDtos) {
        InputInfoDto inputInfoDto = inputInfoDtos.get(0);
        inputInfoService.updateById(inputInfoDto);
        if (inputInfoDto.getIncome() != null) {
            inpIncomeService.updateBatchById(inputInfoDto.getIncome());
        }
        return ReturnResult.ok();
    }

    @PostMapping("/page")
    public ReturnResult page(@RequestBody InputInfoPageParam param) {
        return ReturnResult.ok(inputInfoService
                .page(new Page<>(param.getCurrentPage(), param.getPageSize()), new LambdaQueryWrapper<InputInfo>()
                        .gt(ObjectUtil.isNotEmpty(param.getStartCreateTime()), InputInfo::getCreateTime, param.getStartCreateTime())
                        .le(ObjectUtil.isNotEmpty(param.getEndtCreateTime()), InputInfo::getCreateTime, param.getEndtCreateTime())
                        .gt(ObjectUtil.isNotEmpty(param.getStartStaticDate()), InputInfo::getStaticDate, param.getStartStaticDate())
                        .le(ObjectUtil.isNotEmpty(param.getEndStaticDate()), InputInfo::getStaticDate, param.getEndStaticDate())
                        .in(ObjectUtil.isNotEmpty(param.getScenics()), InputInfo::getScenic, param.getScenics())
                        .eq(ObjectUtil.isNotEmpty(param.getManager()), InputInfo::getManager, param.getManager())
                )
        );
    }

    @PostMapping("/del")
    @ResponseBody
    public ReturnResult del(int id) {
        inpIncomeService.remove(new LambdaQueryWrapper<InpIncome>().eq(InpIncome::getInputInfoId, id));
        return ReturnResult.ok(inputInfoService.removeById(id));
    }

    @PostMapping("/getTypeAuthority")
    public ReturnResult getTypeAuthority(Principal principal) {
        String account = principal.getName();
        Manage manage = manageService.getMangerByAccount(account);
        List<TypeAuthority> typeAuthorities = typeAuthorityService.list(new LambdaQueryWrapper<TypeAuthority>()
                .eq(TypeAuthority::getManager, manage.getId())
                .eq(TypeAuthority::getAuthority, Cons.TYPE_AUTHORITY.ALL));
        if (typeAuthorities.isEmpty()) {
            return ReturnResult.ok();
        }
        return ReturnResult.ok(typeAuthorities.stream().map(TypeAuthority::getScenic).collect(Collectors.toList()));
    }

    /**
     * 获取 income
     *
     * @param id
     * @return
     */
    @PostMapping("/get")
    public ReturnResult get(Integer id) {
        return ReturnResult.ok(inpIncomeService.list(new LambdaQueryWrapper<InpIncome>().eq(InpIncome::getInputInfoId, id)));
    }

}
