package cn.ezhizao.project.business.finance.IndividualIncomeTax.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.finance.IndividualIncomeTax.domain.BizFinancialIndividualIncomeTax;
import cn.ezhizao.project.business.finance.IndividualIncomeTax.domain.BizFinancialIndividualIncomeTaxDetail;
import cn.ezhizao.project.business.finance.IndividualIncomeTax.service.IBizFinancialIndividualIncomeTaxService;
import cn.ezhizao.project.business.finance.IndividualIncomeTaxIsZero.domain.BizFinancialIndividualIncomeTaxDetailIsZero;
import cn.ezhizao.project.business.finance.IndividualIncomeTaxIsZero.domain.BizFinancialIndividualIncomeTaxIsZero;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalary;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetail;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetailEmployee;
import cn.ezhizao.project.business.salary.service.IBizFinancialSalaryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 个税任务Controller
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/business/detail")
public class BizFinancialIndividualIncomeTaxController extends BaseController
{
    @Resource
    private IBizFinancialIndividualIncomeTaxService bizFinancialIndividualIncomeTaxService;

    @Resource
    private IBizFinancialSalaryService bizFinancialSalaryService;

    /**
     * 查询个税任务列表
     */
    @PreAuthorize("@ss.hasPermi('business:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizFinancialIndividualIncomeTax bizFinancialIndividualIncomeTax) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizFinancialIndividualIncomeTax);
        String month=bizFinancialIndividualIncomeTax.getMonth();
        String year=bizFinancialIndividualIncomeTax.getYear();
        //分页数据
        startPage();
        //获取个税任务数据
        List<BizFinancialIndividualIncomeTax> list = bizFinancialIndividualIncomeTaxService.getList(bizFinancialIndividualIncomeTax);
//        //获取个税详情数据
        if(list.size()>0){
            list.forEach(item->{
                BizFinancialIndividualIncomeTaxDetail detail=bizFinancialIndividualIncomeTaxService.getTaxDetail(year,month,item.getId());
                item.setDetail(detail);
            });
        }
        //返回数据
        return getDataTable(list);

    }

    /**
     * 查询工资列表信息
     */
    @PreAuthorize("@ss.hasPermi('business:detail:wageList')")
    @PostMapping("/getDetail")
    public AjaxResult getDetail(@RequestBody BizFinancialIndividualIncomeTax bizFinancialIndividualIncomeTax) throws NoSuchFieldException, IllegalAccessException
    {
        //根据workOrderId获取工资任务信息
        Long  ID=bizFinancialIndividualIncomeTax.getWorkOrderId();
        BizFinancialSalary bizFinancialSalary = bizFinancialIndividualIncomeTaxService.getSalary(bizFinancialIndividualIncomeTax.getWorkOrderId());
        //根据年、月、工资任务、获取月工资单
        BizFinancialIndividualIncomeTaxDetail taxDetail=bizFinancialIndividualIncomeTax.getDetail();
        String year=taxDetail.getYear();
        String month=taxDetail.getMonth();
        BizFinancialSalaryDetail bizFinancialSalaryDetail = bizFinancialIndividualIncomeTaxService.getSalaryDetail(year,month,bizFinancialSalary.getId());
        if(bizFinancialSalaryDetail==null){
            return success();
        }

        //根据月工资单查询工资表
        List<BizFinancialSalaryDetailEmployee> detail = bizFinancialIndividualIncomeTaxService.getSalaryEmployee(bizFinancialSalaryDetail.getId());
        bizFinancialSalaryDetail.setDetails(detail);
        //返回数据
        return success(bizFinancialSalaryDetail);

    }

    /**
     * 导出个税任务列表
     */
    @PreAuthorize("@ss.hasPermi('business:detail:export')")
    @Log(title = "个税任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizFinancialIndividualIncomeTax bizFinancialIndividualIncomeTax) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizFinancialIndividualIncomeTax);
        List<BizFinancialIndividualIncomeTax> list = bizFinancialIndividualIncomeTaxService.getList(bizFinancialIndividualIncomeTax);
        for(BizFinancialIndividualIncomeTax l:list){
            l.setCurrentPeriod(bizFinancialIndividualIncomeTax.getYear()+"-"+bizFinancialIndividualIncomeTax.getMonth());
            BizFinancialIndividualIncomeTaxDetail detail=bizFinancialIndividualIncomeTaxService.getTaxDetail(bizFinancialIndividualIncomeTax.getYear(),bizFinancialIndividualIncomeTax.getMonth(),l.getId());
            if(detail!=null){
                switch (detail.getStatus()){
                    case 0:
                        l.setStatusText("未开始");
                        l.setMakeOut("未填写");
                        break;
                    case 1:
                        l.setStatusText("进行中");
                        l.setMakeOut("进行中");
                        break;
                    case 3:
                        l.setStatusText("已完成");
                        l.setMakeOut("已填写");
                        break;
                    default:
                        break;
                }
            }else{
                l.setStatusText("未开始");
                l.setMakeOut("未填写");
            }
        }
        ExcelUtil<BizFinancialIndividualIncomeTax> util = new ExcelUtil<BizFinancialIndividualIncomeTax>(BizFinancialIndividualIncomeTax.class);
        util.exportExcel(response, list, "个税非零申报数据");
    }

    /**
     * 获取个税任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizFinancialIndividualIncomeTaxService.getById(id));
    }

    /**
     * 新增个税任务
     */
    @PreAuthorize("@ss.hasPermi('business:detail:add')")
    @Log(title = "个税任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizFinancialIndividualIncomeTax bizFinancialIndividualIncomeTax) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizFinancialIndividualIncomeTax);
        return toAjax(bizFinancialIndividualIncomeTaxService.save(bizFinancialIndividualIncomeTax));
    }

    /**
     * 修改个税任务
     */
    @PreAuthorize("@ss.hasPermi('business:detail:edit')")
    @Log(title = "个税任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizFinancialIndividualIncomeTax bizFinancialIndividualIncomeTax) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizFinancialIndividualIncomeTax);
        return toAjax(bizFinancialIndividualIncomeTaxService.updateById(bizFinancialIndividualIncomeTax));
    }

    /**
     * 删除个税任务
     */
    @PreAuthorize("@ss.hasPermi('business:detail:remove')")
    @Log(title = "个税任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizFinancialIndividualIncomeTaxService.removeBatchByIds(ids));
    }
}
